<?php

namespace app\api\controller;
use app\admin\model\{Product,Exchange,ScoreLog,User};
use hg\apidoc\annotation as Apidoc;
use think\facade\Db;

/**
 * @Apidoc\Title("积分商城")
 */
class Score extends ApiBase {

	    /**
     * @Apidoc\Title("商品列表")
     * @Apidoc\Url("/api/score_index")
     * @Apidoc\Method("GET")
     * @Apidoc\Returned("[]", type="list", desc="列表",
        * @Apidoc\Returned("goods_id", type="int", desc="商品id"),
        * @Apidoc\Returned("type", type="int", desc="商品类型 1实物 2虚拟 3其他"),
        * @Apidoc\Returned("thumb", type="string", desc="商品轮播"),
        * @Apidoc\Returned("slide", type="string", desc="轮播图"),
        * @Apidoc\Returned("out_price", type="int", desc="市场价格"),
        * @Apidoc\Returned("score", type="int", desc="所需积分"),
        * @Apidoc\Returned("stock", type="int", desc="库存")
     * )
     */
    public function goods_list(){
    	$data = request()->param();
    	$where = [];
    	if(isset($data['keyword']) && $data['keyword'] !=''){
    		$where[] = ['keyword|store_name','like',"%{$data['keyword']}%"];
    	}
    	$page  = request()->param('page/d',1);
 		$limit = request()->param('limit/d',10);
    	$list  = Product::where('is_show',1)->field('id as goods_id,type,thumb,slide,out_price,score,stock')->where($where)->order('sort desc')->page($page,$limit)->select();
    	json_response(1,'success',$list);
    }

   
 	    /**
     * @Apidoc\Title("商品详情")
     * @Apidoc\Url("/api/score_detail")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("goods_id", type="int", desc="商品id")
     * @Apidoc\Returned("thumb", type="string", desc="商品轮播")
     * @Apidoc\Returned("slide", type="string", desc="轮播图")
     * @Apidoc\Returned("out_price", type="int", desc="市场价格")
     * @Apidoc\Returned("score", type="int", desc="所需积分")
     * @Apidoc\Returned("stock", type="int", desc="库存")
     */
    public function detail(){
    	$goods_id = param_check('goods_id');
    	$goods_info   = Product::where('is_show',1)->where('stock','>',0)->where('id',$goods_id)->field('id as goods_id,thumb,slide,out_price,score,stock,content,stock')->find();
    	$goods_info->content = html_entity_decode($goods_info->content);
    	if(!$goods_info) json_response(0,'商品已下架或不存在');
    	json_response(1,'success',$goods_info);
    }

        /**
     * @Apidoc\Title("商品兑换")
     * @Apidoc\Url("/api/score_exchange")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("goods_id", type="int", desc="商品id",require=true)
     * @Apidoc\Param("number", type="int", desc="兑换数量",require=true)

     */
    public function exchange(){
        $uid      = $this->get_user_id(0);
        $goods_id =  param_check('goods_id');
        $number   = param_check('number');
        $user      = User::find($uid);

        Db::startTrans();
        try {
            if(!$goods = Product::where('stock','>',0)->where('id',$goods_id)->lock(true)->find()) exception('商品不存在或者库存不足');
            if(!$goods->is_show) exception('商品已下架');
            if($user->score  < $goods->score * $number) exception('积分不足');
            if($goods->type == 1){
                //实物 待留资
                $status = 5;
            }elseif($goods->type == 2){
                //虚拟  直接完成 或其他逻辑
                $status = 4;
            }elseif($goods->type ==3){
                //其他 待审核模式
                $status = 0;
            }else{

            }
            Exchange::create([
                'uid'  => $uid,
                'goods_id' => $goods->id,
                'score'    => $goods->score * $number,
                'number'   => $number,
                'type'     => $goods->type,
                'status'   => $status
            ]);
            $goods->stock -=1;
            $goods->save();
            //记录日志
            Exchange::out_score($uid,$goods->score * $number,1);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            json_response(0,$e->getMessage());
        }
        json_response(1,'兑换成功');
    }
 	    /**
     * @Apidoc\Title("兑换记录")
     * @Apidoc\Url("/api/score_log")
     * @Apidoc\Method("GET")
     * @Apidoc\Param("page", type="int", desc="页码")
     * @Apidoc\Param("limit", type="int", desc="数据量")
     * @Apidoc\Returned("goods_thumb", type="string", desc="商品图片")
     * @Apidoc\Returned("out_price", type="int", desc="市场价格")
     * @Apidoc\Returned("score", type="int", desc="所需积分")
     * @Apidoc\Returned("stock", type="int", desc="库存")
     * @Apidoc\Returned("status", type="int", desc="状态 0 待审核 1待发货 2已完成  3审核拒绝  5待留资")
     */
    public function exchangeLog(){
        $uid      = $this->get_user_id();
        $page     = \request()->param('page/d',1);
        $limit     = \request()->param('limit/d',10);
        $list     = Exchange::with(['goods'])->where('uid',$uid)->field('id,goods_id,uid,create_time,score,status')->append(['status_text'])->order('id desc')->page($page,$limit)->select();
        \json_response(1,'success',$list);
    }

    
     	    /**
     * @Apidoc\Title("实物奖品留资")
     * @Apidoc\Url("/api/save_prize")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("id", type="int",require=true, desc="奖品id")
     * @Apidoc\Param("name", type="string", require=true,desc="收件人")
     * @Apidoc\Param("mobile", type="string", require=true,desc="收件人电话")
     * @Apidoc\Param("address", type="string", require=true,desc="收件人地址")
     * @Apidoc\Returned("goods_thumb", type="string", desc="商品图片")
     * @Apidoc\Returned("out_price", type="int", desc="市场价格")
     * @Apidoc\Returned("score", type="int", desc="所需积分")
     * @Apidoc\Returned("stock", type="int", desc="库存")
     * @Apidoc\Returned("", type="string", desc="")
     */
    public function save_prize(){
        $uid      = $this->get_user_id();
        $id       = \param_check('id',false,'缺少id');
        $name     = param_check('name',false,'缺少收件人');
        $mobile   = param_check('mobile',false,'缺少收件人联系方式');
        $address  = param_check('address',false,'缺少收件人收货地址');
        if(!$exchange = Exchange::where('uid',$uid)->where('id',$id)->find()) \json_response(0,'记录不存在');
        if($exchange->status != 5) \json_response(0,'状态异常');
        $exchange->name = $name;
        $exchange->mobile = $mobile;
        $exchange->address = $address;
        $exchange->status = 1;
        if($exchange->save())  json_response(1,'操作成功');
        json_response(0,'操作失败');
    }
}
